package com.yixun.qfbao.hander;

import java.text.SimpleDateFormat;
import java.util.*;

import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.DateStyle;
import com.yixun.qfbao.utils.DateUtils;
import com.yixun.qfbao.utils.LoginUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.api.service.CPService;
import com.yixun.qfbao.apidto.ExportCPReqDto;
import com.yixun.qfbao.apidto.ExportCPResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.vo.ExportCPReqVo;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.model.enums.ConfirmStatusEnum;
import com.yixun.qfbao.xf.model.enums.MortgageStatusEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * 报备导出
 *
 * @author JHSong
 */
@Service("exportCPHander")
public class ExportCPHander extends CommonAbstractHander {

    private static Logger logger = LoggerFactory.getLogger(ExportCPHander.class);

    @Autowired
    private CPService cpService;
    @Autowired
    private LoginUtils loginUtils;

    @SuppressWarnings("rawtypes")
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        ExportCPReqVo req = (ExportCPReqVo) reqDto;
        ExportCPReqDto reqDto1 = req.getParams();
        BaseResponseDTO<ExportCPResDto> res = new BaseResponseDTO<ExportCPResDto>();
        ExportCPResDto resDto = new ExportCPResDto();
        resDto.setResult(ResultEnum.FAIL.getNo());
        try {

            PartnerInfo pi = loginUtils.getUser(reqDto1.getUserId());
            if (pi == null) {
                res.setResult(ResultEnum.USER_IS_NULL.getNo());
                res.setErrorMsg("未找到用户信息");
                return res;
            }
            reqDto1.setAccountType(pi.getAccountType());
            reqDto1.setMerchantId(pi.getHigherid());

            resDto = cpService.exportCP(reqDto1);
            List<CustomerPremisesDto> cpDtoList = resDto.getCpDtoList();

            if (null != cpDtoList && cpDtoList.size() > 0) {
                List<String[]> headerList = new ArrayList<String[]>(); // 表头
				String[] header = new String[]{
						"序号", "楼盘名称", "客户姓名", "客户电话", "报备类型", "报备人姓名", "报备人电话",
						"报备状态", "交易状态", "按揭状态", "经纪公司", "机构门店", "所在部门", "经纪人姓名",
						"经纪人电话", "渠道经理", "渠道经理电话", "报备时间", "期望带看时间", "实际到访时间",
						"是否多次到访", "报备推荐人", "报备推荐人手机号", "楼栋号", "房号","首付交齐",
						"认购时间", "认购金", "签约面积", "签约时间", "成交总价", "客户描述"
				};
                headerList.add(header);
                List<List<Map<String, String>>> customerList = new ArrayList<List<Map<String, String>>>(); // 数据
                List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                int index = 0;
                for (CustomerPremisesDto cpDto : cpDtoList) {
                    index = index + 1;
                    Map<String, String> map2 = new HashMap<String, String>();
                    map2.put(header[0], index + "");
                    map2.put(header[1], cpDto.getPremisesName());
                    map2.put(header[2], cpDto.getName());
                    map2.put(header[3], cpDto.getMobile());
                    map2.put(header[4], cpDto.getReportType());
                    if ("案场代报备".equals(cpDto.getReportType())) {
                        map2.put(header[5], cpDto.getAssistantName());
                        map2.put(header[6], cpDto.getAssistantPhone());
                    } else {
                        map2.put(header[5], cpDto.getStaffName());
                        map2.put(header[6], cpDto.getStaffPhone());
                    }
                    map2.put(header[7], cpDto.getCusNewStatusName());
                    if (!StringUtils.isEmpty(cpDto.getConfirmStatus())) {
                        map2.put(header[8], ConfirmStatusEnum.getByNo(cpDto.getConfirmStatus()).getDes());
                    }
                    if (!StringUtils.isEmpty(cpDto.getMortgageStatus())) {
                        map2.put(header[9], MortgageStatusEnum.getByNo(cpDto.getMortgageStatus()).getDes());
                    }
                    map2.put(header[10], cpDto.getStaffCompanyName());
                    map2.put(header[11], cpDto.getStaffStoreName());
                    map2.put(header[12], cpDto.getStaffDepartmentName());
                    map2.put(header[13], cpDto.getStaffName());
                    map2.put(header[14], cpDto.getStaffPhone());
                    map2.put(header[15], cpDto.getPrincipalName());
                    map2.put(header[16], cpDto.getPrincipalPhone());
                    map2.put(header[17], sdf.format(cpDto.getCreateTime()));
                    map2.put(header[18], "");
                    if (null != cpDto.getHopeVisitTime()) {
                        map2.put(header[18], sdf.format(cpDto.getHopeVisitTime()));
                    }
                    if (null != cpDto.getVisitTime()) {
                        map2.put(header[19], sdf.format(cpDto.getVisitTime()));
                    }
                    if (null != cpDto.getVisitState()) {
                        map2.put(header[20], cpDto.getVisitState());
                    }
                    map2.put(header[21], cpDto.getRecommenderName());
                    map2.put(header[22], cpDto.getRecommenderPhone());
                    map2.put(header[23], cpDto.getBuildingName());
                    map2.put(header[24], cpDto.getRoomName());
					map2.put(header[25], "");
					if("1".equals(cpDto.getPayFinish())){
						map2.put(header[25], "是");
					}else if("0".equals(cpDto.getPayFinish())){
						map2.put(header[25], "否");
					}
					map2.put(header[26],"");
					if(Objects.nonNull(cpDto.getSubscribeTime())){
						map2.put(header[26], sdf.format(cpDto.getSubscribeTime()));
					}
                    map2.put(header[27], Objects.isNull(cpDto.getSignatureAmount())?"":cpDto.getSignatureAmount().toString());
                    map2.put(header[28], Objects.isNull(cpDto.getValuationArea())?"":cpDto.getValuationArea().toString());
					map2.put(header[29],"");
					if(Objects.nonNull(cpDto.getSignTime())){
						map2.put(header[29], sdf.format(cpDto.getSignTime()));
					}
                    map2.put(header[30], Objects.isNull(cpDto.getDealAmount())?"":cpDto.getDealAmount().toString());
                    map2.put(header[31], cpDto.getRemark());
                    mapList.add(map2);
                }
                customerList.add(mapList);
                // 导出头
                resDto.setHeaderList(headerList);
                // 导出数据源
                resDto.setCustomerList(customerList);
                resDto.setResult(ResultEnum.SUCCESS.getNo());
            } else {
                resDto.setResultMsg("数据为空!");
            }
        } catch (Exception e) {
            logger.error("export cphander err: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("导出数据获取失败," + e.getMessage());
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }

}
